/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

/**
 *
 * @author mirko
 */


 import RPS.EquivalenceMapping;
import RPS.MappingAssertion;
import RPS.MappingVariableRenamer;
import RPS.PeerSchema;
import RPS.RPS;
import Rewriting.FederationManager;
import Rewriting.SPARQLEquivalenceChecker;
import SPARQL.Utilities.SPARQLVariableRenamer;
import Rewriting.SPARQLRewriter;
import Rewriting.Source;
import Rewriting.TripleUnifier;
import SPARQL.SPARQL;
import SPARQL.Service;
import SPARQL.SimpleURI;
import SPARQL.TriplePattern;
import SPARQL.Utilities.QueryRenamer;
import SPARQL.Utilities.SPARQLqueryTranslator;
import SPARQL.Variable;
import static Test.TestQuery.serviceEndpoint;
import arq.query;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import java.util.HashSet;
import parser.ParseException;
import parser.SparqlParser;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.ResultSetRewindable;
import com.hp.hpl.jena.sparql.sse.SSE;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author riccardo
 */
public class TestQueryTime {
    
 
    
    public static void main(String[] args) throws ParseException, FileNotFoundException, UnsupportedEncodingException, IOException {
        
        
        
       
       
        testOriginal();
    }
    
    
    private static void testOriginal() throws IOException, FileNotFoundException, UnsupportedEncodingException {
        Set<String> setEndpoints = new HashSet<>();
        setEndpoints.add("http://dbpedia.org/sparql");
        setEndpoints.add("http://data.linkedmdb.org/sparql");
        setEndpoints.add("http://factforge.net/sparql");
        String queryfileNumber ="5";
        ExecutionQuery exeq = new ExecutionQuery();
        PrintWriter writer = new PrintWriter("timequeryOriginal"+ queryfileNumber+".txt", "UTF-8");
        Date date = new Date();
        System.out.println("Date: "+ date);
        writer.println("Date: "+ date);
        
        
        //String stringquery = readFile("query"+queryfileNumber+".txt");
        
        Query query = QueryFactory.create(TestQuery1.query5);
        System.out.println(query);
        System.out.println("Query"+queryfileNumber);
        for (String endpoint: setEndpoints){
            System.out.println("Endpoint: " + endpoint);
            writer.println("Endpoint: " + endpoint);
            long sum = 0;
            int j = 0;
        for (int i=1 ; i<=10 ; i++) {
            j++;
            long timeBegin = System.currentTimeMillis();
            exeq.executeQueryTestEndpointNoPrint(query, endpoint);
            long timeEnd = System.currentTimeMillis();
            System.out.println(timeEnd-timeBegin);
            writer.println(timeEnd-timeBegin);
            sum += timeEnd-timeBegin;
        }
        writer.println("Average: " +sum/j);
        System.out.println("Average: " +sum/j);
        }
        
        writer.close();
    }

    private static void testRewritten() throws IOException, FileNotFoundException, UnsupportedEncodingException {
        String queryfileNumber ="5";
        ExecutionQuery exeq = new ExecutionQuery();
        PrintWriter writer = new PrintWriter("timequery"+ queryfileNumber+".txt", "UTF-8");
        Date date = new Date();
        System.out.println("Date: "+ date);
        writer.println("Date: "+ date);
        
        
        long sum = 0;
        int j = 0;
        String stringquery = readFile("query"+queryfileNumber+".txt");
        
        Query query = QueryFactory.create(stringquery);
        System.out.println(query);
        System.out.println("Query"+queryfileNumber);
        for (int i=1 ; i<=10 ; i++) {
            j++;
            long timeBegin = System.currentTimeMillis();
            exeq.executeQueryTestNoPrint(query);
            long timeEnd = System.currentTimeMillis();
            System.out.println(timeEnd-timeBegin);
            writer.println(timeEnd-timeBegin);
            sum += timeEnd-timeBegin;
        }
        writer.println("Average: " +sum/j);
        writer.close();
    }
    
        
    private static String readFile( String file ) throws IOException {
    BufferedReader reader = new BufferedReader( new FileReader (file));
    String         line = null;
    StringBuilder  stringBuilder = new StringBuilder();
    String         ls = System.getProperty("line.separator");

    while( ( line = reader.readLine() ) != null ) {
        stringBuilder.append( line );
        stringBuilder.append( ls );
    }

    return stringBuilder.toString();
}
 
}
    
    


